package daoImpl;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import dao.CommentDAO;
import model.Comment;

@Repository("commentDAOImpl")
public class CommentDAOImpl implements CommentDAO{

	@Override
	public List<Comment> getComments(int expID) {
		
		Connection conn;
		int exp = expID;
		boolean success = false;
		List <Comment> commentList = new ArrayList<Comment>();
		try {
			conn = util.DatabaseUtils.dbConnection();
			String query = "SELECT * FROM Exp_Comments WHERE expid="+exp+"";
			Statement  statement = conn.createStatement();
			ResultSet rs = statement.executeQuery(query);
			while(rs.next()){
				int comID = rs.getInt("cid");
				String commentQuery = "SELECT * FROM Comment WHERE id="+comID+"";
				Statement st = conn.createStatement();
				ResultSet comSet = st.executeQuery(commentQuery);
				Comment c;
				if(comSet.next()){
					String text = comSet.getString("text");
					int userID = comSet.getInt("userid");
					int likes = comSet.getInt("likes");
					int dislikes = comSet.getInt("dislikes");
					Date date = comSet.getDate("date");
					c = new Comment(text,userID,likes,dislikes,date);
					commentList.add(c);
					success =true;
				}
			}
			
		} catch (SQLException e) {
			System.out.println("Database error" + e.toString());
		} catch (Exception e) {
			System.out.println("Database error" + e.toString());
		}
		
		
		return commentList;
	}
		
}
